package by.bsu.fpmi.bbtp.impl;

import by.bsu.fpmi.bbtp.controllers.ClientController;
import by.bsu.fpmi.bbtp.dao.ClientDAO;
import by.bsu.fpmi.bbtp.entities.Client;
import by.bsu.fpmi.bbtp.entities.ClientHead;
import org.springframework.beans.factory.annotation.Autowired;

import javax.jws.WebParam;
import javax.jws.WebService;
import java.util.ArrayList;
import java.util.List;

@WebService(endpointInterface = "by.bsu.fpmi.bbtp.controllers.ClientController")
public class ClientControllerImpl implements ClientController{

    @Autowired
    private ClientDAO clientDao;

    @Override
    public void saveClient(String sessionId, Client client) {
        clientDao.save(client);
    }

    @Override
    public Client getClient(String sessionId, long clientId) {
        return clientDao.find(clientId);
    }

    @Override
    public List<ClientHead> getClientsList(String sessionId) {
        return getHeads(clientDao.all());
    }

    @Override
    public void deleteClient(String sessionId, long clientId) {
        Client client = clientDao.find(clientId);
        clientDao.remove(client);
    }

    @Override
    public boolean clientExists(String sessionId, String email) {
        return clientDao.findByEmail(email) != null;
    }

    @Override
    public Client findByEmail(String sessionId, String email) {
        return clientDao.findByEmail(email);
    }

    private List<ClientHead> getHeads(List<Client> clients) {
        List<ClientHead> heads = new ArrayList<ClientHead>();
        for (Client client : clients) {
            heads.add(client.getHead());
        }
        return heads;
    }
}
